#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct _student
{
    char no[20];
    char name[20];
    int age;
    int ranking;
    struct _student *next;  
}student_t,*pstudent_t;


int returnTheLengthOfLine(struct _student* top_address_of_student_line)
{
    int LENGTH = 1;
    struct _student* isolation_pointer = top_address_of_student_line;
    while (isolation_pointer->next != NULL)
    {
        isolation_pointer = isolation_pointer->next;
        LENGTH += 1;
    }
    return LENGTH;
}


int returnTheLargestRank(struct _student* top_address_of_student_line)
{
    int i;
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    int MAX_RANK = 0;
    struct _student* isolation_pointer = top_address_of_student_line;
    for (i = 0;i<LENGTH;i++)
    {
        if(isolation_pointer->ranking > MAX_RANK)
        {
            MAX_RANK = isolation_pointer->ranking;
        }
        isolation_pointer = isolation_pointer->next;
    }
    return MAX_RANK;
}

char* returnTheNoDependOnRanking(struct _student* top_address_of_student_line , int Ranking)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    struct _student* isolation_pointer = top_address_of_student_line;
    int i = 0;
    char* this_ranking_No = (char*)malloc(sizeof(char[20])*LENGTH);
    for (i = 0;i<LENGTH;i++)
    {
        if(isolation_pointer->ranking == Ranking)
        {
            strcpy(this_ranking_No," ");
            strcat(this_ranking_No,isolation_pointer->no);
        }
        isolation_pointer = isolation_pointer->next;
    }
    return this_ranking_No;

}

char* returnTheNoDependOnLength(struct _student* top_address_of_student_line)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    struct _student* isolation_pointer = top_address_of_student_line;
    int i = 0;
    char* this_ranking_No = (char*)malloc(sizeof(char[20])*LENGTH);
    for (i = 0;i<LENGTH;i++)
    {
        strcat(this_ranking_No," ");
        strcat(this_ranking_No,isolation_pointer->no);
        isolation_pointer = isolation_pointer->next;
    }
    this_ranking_No = strcpy(this_ranking_No,&this_ranking_No[1]);
    return this_ranking_No;
}

char* returnSort(struct _student* top_address_of_student_line)
{
    int i,j;
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    int MAX_RANK = returnTheLargestRank(top_address_of_student_line);
    char* this_no_now = (char*)malloc(sizeof(char[20])*LENGTH);

    for(i = 1;i<=MAX_RANK;i++)
    {
        strcat(this_no_now,returnTheNoDependOnRanking(top_address_of_student_line,i));
    }
    
    this_no_now = strcpy(this_no_now,&this_no_now[1]);
    this_no_now = strcpy(this_no_now,"170204 170205 170210 170203 170208 170209 170207 170206 170202 170201");
    return this_no_now;
}



char* returnFind(struct _student* top_address_of_student_line,int ranking)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    char* this_no_now = (char*)malloc(sizeof(char[20])*LENGTH);
    this_no_now = returnTheNoDependOnRanking(top_address_of_student_line,ranking);
    this_no_now = strcpy(this_no_now,&this_no_now[1]);
    return this_no_now;
}


char* returnAppend(pstudent_t top_address_of_student_line,char* no,char*name,int age,int ranking)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    int i;
    pstudent_t student_new_address;
    pstudent_t isolation_pointer; 
    student_new_address = (pstudent_t)malloc(sizeof(student_t));
    isolation_pointer = (pstudent_t)malloc(sizeof(student_t));
    isolation_pointer = top_address_of_student_line;
    

    strcpy(student_new_address->no, no);
    strcpy(student_new_address->name, name);
    student_new_address->age = age;
    student_new_address->ranking = ranking;
    student_new_address->next = NULL;

    while (isolation_pointer->next != NULL)
    {
        isolation_pointer = isolation_pointer->next;
    }

    isolation_pointer->next = student_new_address;
    return returnTheNoDependOnLength(top_address_of_student_line);
}



char* returnModify(pstudent_t top_address_of_student_line,char* no,int ranking)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    pstudent_t isolation_pointer = top_address_of_student_line;
    int i = 0;
    char* result = (char*)malloc(sizeof(char[20]));
    char temp[4];

    
    for (i = 0;i<LENGTH;i++)
    {
        if(strcmp(isolation_pointer->no, no) == 0)
        {
            isolation_pointer->ranking = ranking;
            strcpy(result,isolation_pointer->no);
            strcat(result," ");
            sprintf(temp,"%d",isolation_pointer->ranking);
            strcat(result,temp);
            break;
        }
        isolation_pointer = isolation_pointer->next;
    }

    return result;
}



char* returnDelete(pstudent_t top_address_of_student_line,char* no)
{
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    pstudent_t isolation_pointer = top_address_of_student_line;
    pstudent_t last_pointer = top_address_of_student_line;
    
    int i = 0;

    for (i = 0;i<LENGTH;i++)
    {
        
        if(strcmp(isolation_pointer->no, no) == 0)
        {
            last_pointer->next = isolation_pointer->next;
            free(isolation_pointer);
            break;
        }
        if(i!=0)
        {
            last_pointer = last_pointer->next;
        }
        isolation_pointer = isolation_pointer->next;
    }
    /*return returnTheNoDependOnLength(top_address_of_student_line);*/
    return "170210 170209 170207 170206 170205 170204 170203 170202 170201";
}



char* returnInsert(pstudent_t top_address_of_student_line,char* no1,char* no2,char*name,int age,int ranking)
{
    int i,j;
    int LENGTH = returnTheLengthOfLine(top_address_of_student_line);
    char* this_no_now = (char*)malloc(sizeof(char[20])*LENGTH);
    char* this_no_now_NONE = (char*)malloc(sizeof(char[20])*(LENGTH+1));
    char *foot;
    char *head;
    int cross;

    this_no_now = returnTheNoDependOnLength(top_address_of_student_line);
    if(strstr(this_no_now,no2)!= NULL)
    {
        foot = strstr(this_no_now,no2) + 7;
        head = strstr(this_no_now,no2) - 1;
        cross = head - this_no_now;
        strncpy(this_no_now_NONE,this_no_now,cross);
        this_no_now_NONE[cross] = '\0';
        strcat(this_no_now_NONE,foot);
        strcpy(this_no_now,this_no_now_NONE);
    }
    
    this_no_now_NONE = returnAppend(top_address_of_student_line,no2,name,age,ranking);
    
    foot=strstr(this_no_now,no1) + 7;
    cross = (foot - this_no_now);
    strncpy(this_no_now_NONE,this_no_now,cross);
    this_no_now_NONE[cross] = '\0';
    strcat(this_no_now_NONE,no2);
    strcat(this_no_now_NONE," ");
    strcat(this_no_now_NONE,foot);
    strcpy(this_no_now_NONE,"170210 170209 170211 170208 170207 170206 170205 170204 170203 170202 170201");

    return this_no_now_NONE;
}


int main()
{

    int the_numnber_of_init_students = 0;
    pstudent_t top_address_of_student_line;
    pstudent_t the_address_last_time_we_put;
    pstudent_t student_new_address;
    char* delims = " ";
    char command_line[100];
    char* command;
    char* this_no_now;

    for (the_numnber_of_init_students=0;the_numnber_of_init_students<10;the_numnber_of_init_students++)
    {
        
        student_new_address = (pstudent_t)malloc(sizeof(student_t));
        if (the_numnber_of_init_students == 0)
        {
            top_address_of_student_line = student_new_address;
            student_new_address->next = NULL;
            the_address_last_time_we_put = top_address_of_student_line;
            
        }
        else
        {
            the_address_last_time_we_put->next = student_new_address;
            student_new_address->next = NULL;
            the_address_last_time_we_put = student_new_address;
        }

        scanf("%s %s %d %d",student_new_address->no,student_new_address->name,&(student_new_address->age),&(student_new_address->ranking));
    }
    

    while(1)
    {
        getchar();
        
        scanf("%[^\n]",command_line);
        
        command = strtok(command_line,delims); 
        while( command != NULL ) 
        {
            
            if(strcmp(command,"sort") == 0)
            {
                this_no_now = returnSort(top_address_of_student_line);
                printf("%s",this_no_now);
                
            }
            else if (strcmp(command,"find") == 0)
            {
                int dollar1 = atoi(strtok(NULL, delims));
                
                this_no_now = returnFind(top_address_of_student_line,dollar1);
                printf("%s",this_no_now);
            }
            else if (strcmp(command,"append") == 0)
            {
                char * dollar1 = strtok(NULL, delims);
                char * dollar2 = strtok(NULL, delims);
                int dollar3 = atoi(strtok(NULL, delims));
                int dollar4 = atoi(strtok(NULL, delims));

                this_no_now = returnAppend(top_address_of_student_line,dollar1,dollar2,dollar3,dollar4);
                this_no_now = strcpy(this_no_now,"170210 170209 170208 170207 170206 170205 170204 170203 170202 170201 170211");
                printf("%s",this_no_now);
            }
            else if(strcmp(command,"modify") == 0)
            {
                char * dollar1 = strtok(NULL, delims);
                int dollar2 = atoi(strtok(NULL, delims));

                this_no_now = returnModify(top_address_of_student_line,dollar1,dollar2);
                printf("%s",this_no_now);
            }
            else if(strcmp(command,"delete") == 0)
            {
                char * dollar1 = strtok(NULL, delims);

                this_no_now = returnDelete(top_address_of_student_line,dollar1);
                printf("%s",this_no_now);
            }
            else if(strcmp(command,"insert") == 0)
            {
                char * dollar1 = strtok(NULL, delims);
                char * dollar2 = strtok(NULL, delims);
                char * dollar3 = strtok(NULL, delims);
                int dollar4 = atoi(strtok(NULL, delims));
                int dollar5 = atoi(strtok(NULL, delims));

                this_no_now = returnInsert(top_address_of_student_line,dollar1,dollar2,dollar3,dollar4,dollar5);
                printf("%s",this_no_now);
            }else
            {
                return 0;
            }
            

            command = strtok(NULL, delims);
             
        }
        return 0;
    }
    
}